﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using Code.Models;
using Code.Commen;
using System.Security.Cryptography;

namespace QianBuo.SchemeTool.Commen
{
    public class Management
    {
        //获取验证码的激活码
        public static string GetRegID()
        {
            try
            {
                ////CPU序列号
                //string cpuid = "";
                ////主板信息
                //string boardid = "";
                //网卡物理地址
                string macAddress = "";
                ////c盘卷标号
                //string VolumeSerialNumber = "";
                #region 获取主机的cpuid
                //ManagementObjectSearcher my = new ManagementObjectSearcher("Select * from Win32_Processor");
                //foreach (ManagementObject mo in my.Get())
                //{
                //    try
                //    {
                //        cpuid = mo["ProcessorID"].ToString();
                //    }
                //    catch { }
                //}
                #endregion

                #region 获取主板信息
                //my = new ManagementObjectSearcher("Select * FROM Win32_BaseBoard");
                //foreach (ManagementObject share in my.Get())
                //{
                //    //label1.Text = "主板制造商：" + share["Manufacturer"].ToString();
                //    //label2.Text = "产品：" + share["Product"].ToString();
                //    //label3.Text = "主板序列号：" + share["SerialNumber"].ToString();
                //    try{
                //    boardid = share["Manufacturer"].ToString() ;
                //    }
                //    catch { } try
                //    {
                //        boardid += share["Product"].ToString() ;
                //    }
                //    catch { } try
                //    {
                //        boardid += share["SerialNumber"].ToString();
                //    }
                //    catch { }
                //}

                #endregion

                #region 获取网卡物理地址
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                string[] MacAddress = new string[moc.Count];
                int ii = 0;
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        MacAddress[ii] = mo["MacAddress"].ToString();
                        ii++;
                    }
                    mo.Dispose();
                }
                macAddress = MacAddress[0];
                #endregion


                #region 获取c盘卷标号
                // ManagementObject disk = new ManagementObject("Win32_logicaldisk.deviceid=\"c:\"");
                // disk.Get();
                //VolumeSerialNumber = disk.GetPropertyValue("VolumeSerialNumber").ToString();
                #endregion
                var md5 = MD5.Create();
                var id = md5.ComputeHash(macAddress, "ab1230123ab");
                id = new Guid(id).ToString().ToUpper();
                return id;
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("无法访网络！", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                System.Environment.Exit(0);
            }
            return null;
        }
    }
}
